package com.jzr.medical.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jzr.common.entity.vo.Result;
import com.jzr.common.util.U;
import com.jzr.medical.db1.model.TSysJob;
import com.jzr.medical.db1.service.SysJobService;
import com.jzr.medical.db1.vo.SysJobVo;
import com.jzr.medical.util.RestTemplateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/sysJob")
public class SysJobController {
    @Value("${taskServer.url}")
    private String taskServerUrl;

    @Autowired
    private SysJobService sysJobService;

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/list")
    public Result<List<SysJobVo>> list(SysJobVo record){
        List<SysJobVo> liveList = null;
        ParameterizedTypeReference<List<SysJobVo>> typeRef = new ParameterizedTypeReference<List<SysJobVo>>() {};
        String msg = "";
        try {
            String url = taskServerUrl + "api/job/list";
            liveList = RestTemplateUtil.exchange(restTemplate,url,HttpMethod.GET,typeRef,record);
        }catch (Exception e){
            msg = e.getMessage();
        }
        QueryWrapper<SysJobVo> qw = new QueryWrapper<>();
        qw.orderByAsc("job_name");
        qw.likeRight(U.isNotBlank(record.getJobName()),SysJobVo.JobName_,record.getJobName())
                .likeRight(U.isNotBlank(record.getJobClass()),SysJobVo.JobClass_,record.getJobClass());

        List<SysJobVo> list = sysJobService.list(qw);
        if(liveList != null && liveList.size() > 0) {
            for (TSysJob job : list) {
                boolean isFound = false;
                for(TSysJob live : liveList){
                    if(job.getId().equals(live.getId())){
                        job.setIsStartPlan(true);
                        job.setIsDone(live.getIsDone());
                        job.setIsCancelled(live.getIsCancelled());
                        isFound = true;
                    }
                }
                if(!isFound){
                    job.setIsStartPlan(false);
                    job.setIsDone(true);
                    job.setIsCancelled(false);
                }
            }
        }else{
            for(TSysJob job :list){
                job.setIsStartPlan(false);
                job.setIsDone(true);
                job.setIsCancelled(false);
            }
        }
        return Result.success("",list);
    }

    @PostMapping("/saveBatch")
    public Result<List<SysJobVo>> saveBatch(@RequestBody List<SysJobVo> list){
        List<SysJobVo> result = sysJobService.saveBatch(list);
        return Result.success("成功",result);
    }

    @GetMapping("/start")
    public Result<Boolean> start(Integer id){
        String url = taskServerUrl + "api/job/start?id={id}";
        Result<Boolean> result = null;
        try {
            Map<String,Object> param = new HashMap<>();
            param.put("id",id);
            result = restTemplate.getForObject(url, Result.class,param);
            return result;
        }catch (Exception e){
            return Result.fail("失败," + e.getMessage());
        }
    }

    @GetMapping("/stop")
    public Result<Boolean> stop(Integer id,Boolean isForceStop){
        String url = taskServerUrl + "api/job/stop?id={id}&isForceStop={isForceStop}";
        Result<Boolean> result = null;
        try {
            Map<String,Object> param = new HashMap<>();
            param.put("id",id);
            param.put("isForceStop",isForceStop);
            result = restTemplate.getForObject(url, Result.class,param);
            return result;
        }catch (Exception e){
            return Result.fail("失败," + e.getMessage());
        }
    }

    @GetMapping("/getErrLog")
    public Result<String> getErrLog(Integer id){
        if(id == null){
            return Result.fail("参数id不能为空");
        }
        return Result.success("成功",sysJobService.getErrLog(id));
    }

}
